<template>
	<view class="container">
		<view id="canvasId" class="content" v-if="orders!= null">
			<view class="title">
				{{orders.merchantName || null}}
			</view>
			<view class="date">
				{{orders.date || null}}
			</view>
			<view class="body">
				<view class="good" v-for="item in good" :key="item.id">
					<view class="left">
						{{item.shopName}}
					</view>
					<view class="right">
						<text>{{item.shopNumber}}</text>X
						<text>{{item.shopPrice}}</text>元/
						<text>{{item.shopSpec}}</text> = 
						<text>{{item.shopAmount}}</text>元
					</view>
				</view>
				<view class="sum">
					<view class="texts">
						总计：<text class="sumNmuber">{{sum}}</text>
					</view>
				</view>
				
			</view>
			
			<view class="sign">
				<image class="signs" :src="orders.sign" mode="widthFix"></image>
				<!-- <image class="image" src="http://192.168.1.109:8080/profile/upload/2025/06/13/微信图片_20240722134947_20250613171339A004.jpg" mode="widthFix"></image> -->
			</view>
		</view>
		<!-- 收据页生成 -->
		<view id="canvasId2" class="receipt" v-else>
			<view class="top">
				<view class="left">
					<view class="">
						收
					</view>
					<view class="">
						据
					</view>
				</view>
				<view class="right">
					日期：{{ now }}
				</view>
			</view>
			<view class="bianhao">
				NO: {{(detail.id+'').padStart(6,'0')}}
			</view>
			
			<view class="text">
				<view class="first">
					<view class="intent">
						今收到
					</view>
					<view class="shopName" decode="true" v-html="shopNameText">
					</view>
				</view>
				<view class="second">
					
					<view style="width: 100%;text-decoration: underline;" decode="true" v-html="typeText">
					</view>
					
				</view>
				<view class="three">
					<view class="">金额（大写）</view>
					<view class="sprice" v-for="(item,index) in unit" :key="'unit'+index">
						<view class="price">
							{{textAmount[index] == '零'? '/':textAmount[index]}}
						</view>
						<view class="spec">
							{{item}}
						</view>
					</view>
				</view>
				<view class="four">
					<view class="left">
						<view class="">
							备注：
						</view>
						<view class="beizhu">
							{{detail.start+' 至 '+detail.end}}
						</view>
						
					</view>
					<view class="right">
						￥：
						<view class="price">
							{{(detail.amount+"")}}
						</view>
					</view>
				</view>
			</view>
			
			<view class="bottom">
				<view>
					收款单位：
				</view>
				<view style="width: 150rpx;">
					经手人：
				</view>
			</view>
		</view>
		<sp-html2canvas-render :reverse="orders!= null ? 0:1" :domId="orders!= null ?'canvasId':'canvasId2'" ref="renderRef" @renderOver="renderOver" /></sp-html2canvas-render>
	</view>
</template>

<script>
	const units = ['分', '角', '元', '十', '百', '千', '万', '十', '百'];
	import { getNowDate,numberToChineseFull } from '../../../utils/utils'
	export default{
		name:'canvasView',
		data(){
			return {
				now: getNowDate(),
				unit:units.reverse()
			}
		},
		props:{
			order:{
				type: Object,
				default:null
			},
			goods:{
				type:Array,
				default:null
			},
			detail:{
				type:Object,
				default:null
			}
		},
		computed:{
			orders(){
				console.log("order===>",this.order)
				return this.order
			},
			good(){
				console.log("goods===>",this.goods)
				return this.goods
			},
			sum(){
				let sum = 0
				if(this.good != null){
					this.good.forEach(item=>{
						sum+=item.shopAmount
					})
				}
				return sum
			},
			shopNameText(){
				console.log(this.detail.shopName)
				return this.str(this.detail.shopName,110)
			},
			typeText(){
				return this.str(this.detail.type,120)
			},
			textAmount(){
				console.log("textAmount===>",numberToChineseFull(this.detail.amount))
				if(this.detail.amount>0){
					return numberToChineseFull(this.detail.amount)
				}else{
					return 0
				}
			}
		},
		mounted() {
			
		},
		methods:{
			renderOver(e){
				this.$emit("renderOver",e)
			},
			str(str,allSize){
				console.log('str: ',str);
				let result = ""
				if(str!='' && str!=null){
					let nameArr = [...str]
					let strLength = nameArr.length*4  //汉字占4个字节
					let ava = (allSize - strLength)/2
					result = str.padEnd(ava+nameArr.length," ").padStart(allSize-nameArr.length*3 ," ");
				}else{
					result = (str+'').padStart(allSize," ")
				}
				result = result.replace(/ /g, '&nbsp;')
				return result;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container{
		// width: 100%;
		// height: 100%;
		
		.content{
			width: 100%;
			display: flex;
			flex-direction: column;
			
			.title{
				width: 100%;
				font-size: 40rpx;
				text-align: center;
			}
			.body{
				margin-top: 30rpx;
				font-size: 35rpx;
			}
			.good{
				display: flex;
				height: 60rpx;
				padding-left: 30rpx;
				line-height: 60rpx;
				
				.left{
					width: 150rpx;
				}
				.right{
					margin-left: 50rpx;
					text{
						margin-left: 10rpx;
						margin-right: 10rpx;
					}
				}
			}
			.sum{
				margin-top: 30rpx;
				padding-left: 10rpx;
			}
			.date{
				text-align: right;
				font-size: 30rpx;
				padding-right: 80rpx;
				/* margin-left: 80rpx; */
			}
			
			.sign{
				position: relative;
				top: -150rpx;
				left: 30%;
				.signs{
					width: 100rpx;
					height: 100rpx;
					transform: rotate(90deg)
				}
			}
		}
		
		.receipt{
			border:1rpx solid #000;
			// padding-bottom: 500rpx;
			background-color: #fff;
			// position: absolute;
			// right:-150rpx;
			// top: 150rpx;
			width: 1200rpx;
			padding:  30rpx 30rpx;
			// height: 100rpx;
			// margin-bottom: 100rpx;
			transform: rotate(90deg);
			display: flex;
			flex-direction: column;
			align-items: center;
			.top{
				display: flex;
				flex-direction: column;
				width: 375rpx;
				
				.left{
					display: flex;
					flex-direction: row;
					align-items: center;
					justify-content: space-evenly;
					font-size: 32rpx;
					font-weight: 600;
				}
				
			}
			
			.bianhao{
				width: 80%;
				text-align: right;
				margin-top: 20rpx;
			}	
			
			.text{
				
				margin-top: 20rpx;
				// width: 81%;
				padding: 40rpx;
				display: flex;
				border: 1rpx solid #909292;
				flex-direction: column;
				
				.first ,.second,.three,.four{
					display: flex;
					line-height: 80rpx;
					.shopName{
						text-decoration: underline;
						font-size: 30rpx;
						font-weight: 600;
					}
					.intent{
						text-indent: 32rpx;
					}
				}
				.price{
					width: 45rpx;
					font-size: 30rpx;
					font-weight: 600;
					text-align: center;
				}
				.three{
					.sprice{
						display: flex;
						
					}
				}
				
				.four{
					width:100%;
					
					.left{
						flex: 1;
						display: flex;
						flex-direction: row;
					}
					.right{
						width: 100%;
						flex: 1;
						display: flex;
						justify-content: end;
						text-align: right;
						
						.price{
							text-decoration: underline;
						}
						
					}
				}
			}
		
			.bottom{
				display: flex;
				margin-top:20rpx;
				border: none;
				width: 80%;
				align-items: center;
				justify-content: space-between;
			}
			
		}
	}
</style>